import pathToRegexp from 'path-to-regexp/index';
import {getStudy,getMaillist,postStudy,putStudy,deleteStudy} from 'services/app';
export default {
    namespace: 'law',
    state: {
        pages:1,
        record:{},
        catalog_id:'1',
        title:'',
    },
    subscriptions: {
        setupHistory ({ dispatch, history }) {
            history.listen((location) => {
                const match = pathToRegexp('/home').exec(location.pathname);
                switch (location.pathname)
                // switch ( "/" + location.href.split("/")[location.href.split("/").length - 1 ])
                {
                case '/Lawlearningfa':
                    dispatch({type:'save',payload:{
                        catalog_id:'1',
                        title:'法规制度',
                        allobj: [{id:'1',name:"国家"},{id:'2',name:"华电公司"},{id:'3',name:"湖北公司"},{id:'4',name:"本公司"}],
                    }});
                    break;
                case '/Lawlearningying':
                    dispatch({type:'save',payload:{
                        catalog_id:'5',
                        title:'应急科普',
                        allobj: [{id:'5',name:"应急科普"}],
                    }});
                    break;
                case '/Lawlearningzai':
                    dispatch({type:'save',payload:{
                        catalog_id:'6',
                        title:'灾难警示',
                        allobj: [{id:'6',name:"灾难警示"}],
                    }});
                    break;
                case '/Lawlearningan':
                    dispatch({type:'save',payload:{
                        catalog_id:'7',
                        title:'应急预案',
                        allobj: [
                            {id:'7',name:"国家应急预案"},
                            {id:'8',name:"地方应急预案"},
                            {id:'9',name:"华电集团预计预案"},
                            {id:'10',name:"湖北公司应急预案"},
                            {id:'11',name:"本地公司应急预案"},
                        ],
                    }});
                    break;
                case '/LawlearninganK':
                    dispatch({type:'save',payload:{
                        catalog_id:'12',
                        title:'应急处置卡',
                        allobj: [
                            {id:'12',name:"应急管理委员会及其成员应急处置卡"},
                            {id:'13',name:"应急指挥部及其成员应急处置卡"},
                            {id:'14',name:"重点岗位应急处理卡"},
                            {id:'15',name:"设备生产事件应急处理卡修订"},
                            {id:'16',name:"应急设备操作（使用）卡"},
                            {id:'17',name:"人身伤害应急救护卡"},
                        ],
                    }});
                    break;
                }
            });
        },
        setup ({ dispatch }) {},
    },
    effects: {
        //查看登录状态
        *getStudy ({ payload }, { call, put }){
            const {data,total} = yield call(getStudy, payload);
            yield put({ type: 'save', payload: {getStudyData:data,total:total,pages:payload.pageNumber}});
        },
        //新增查询
        *newpost  ({ payload }, { call, put }){
            const data = yield call(getMaillist, {
                name:"",
                phone :"",
                pageNumber :0,
                pageSize :0,
            });
            yield put({ type: 'save', payload: {getTXLData:data.data}});
        },
        //添加法规
        *postStudy ({ payload }, { call, put }){
            const data = yield call(postStudy, payload);
            if(data.code == 0){
                yield put({ type: 'save', payload: {view:false,pages:0}});
            }
        },
        //删除法规
        *deleteStudy ({ payload }, { call, put }){
            const data = yield call(deleteStudy, payload);
            if(data.code == 0){
                yield put({ type: 'save', payload: {view:false,pages:0}});
            }
        },
        //修改法规
        *putStudy ({ payload }, { call, put }){
            const data = yield call(putStudy, payload);
            if(data.code == 0){
                yield put({ type: 'save', payload: {view:false,pages:0}});
            }
        },
    },
    reducers: {
        save (state, { payload }) {
            return { ...state, ...payload };
        },
    },
};
